$(function() {
	 var count = 60; //间隔函数，1秒执行
	 var InterValObj; //timer变量，控制时间
	 var curCount;//当前剩余秒数
	 var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
	 var allCode;
	
	$("#phoneCode").click(function() {
		var phone = $("#phone").val();
		if(phoneReg.test(phone)){
//			alert(phone)
			sendMessage1($(this));
			sendCode("Sms",phone);
			showMsg('发送成功！','top','successful');
		}else{
			showMsg('请输入正确的手机格式！','top','error');
		}
	})
	
	$("#login1").click(function() {
		var phone = $("#phone").val();
		var code = $("#code").val();
		if (phone === "") {
			showMsg('手机号不能为空！','top','error');
		}else if(code != allCode){
			showMsg('验证码输入错误！','top','error');
		}else{
			$.ajax({
		        type: "POST",
		        url: "/phoneLoginUser",
		        data: {
		        	"phone":phone,
		        },
		        dataType:"json",
		        success: function(result){
		        	console.log(result);
		        	if (result != false) {
		        		window.location.href = "/shopIndex";
					}else{
						showMsg('无此用户！','top','error');
					}
		        },
		        error:function (xml) {
		          console.log(0);
		        }
			});
		}
	})
	
	$("#login2").click(function() {
		var name = $("#name").val();
		var pwd = $("#pwd").val();
		if (name === "") {
			showMsg('用户名不能为空！','top','error');
		}else if(pwd === ""){
			showMsg('密码不能为空！','top','error');
		}else{
			$.ajax({
		        type: "POST",
		        url: "/doLoginUser",
		        data: {
		        	"name":name,
		        	"pwd":pwd
		        },
		        dataType:"json",
		        success: function(result){
		        	console.log(result);
		        	if (result != false) {
                window.location.href = "/shopIndex";
					}else{
						showMsg('用户名或账号错误！','top','error');
					}
		        },
		        error:function (xml) {
		          console.log(0);
		        }
			});
		}
	})
	
	function sendCode(operate,data) {
	  $.ajax({
	        type: "POST",
	        url: "/register/sendCode",
	        data: {
	        	"operate":operate,
	        	"data":data
	        	},
	        dataType:"json",
	        success: function(result){
	        	console.log(result)
	        	allCode = result;
	        },
	        error:function (xml) {
	          console.log(0);
	        }
	  });
	}
	
	function sendMessage1(selector) {
	    curCount = count;
	    //设置button效果，开始计时
	    selector.attr("disabled", "true");
	    selector.val( + curCount + "秒");
	    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
	    //向后台发送处理数据

  }
  function SetRemainTime() {
    if (curCount == 0) {
      window.clearInterval(InterValObj);//停止计时器
      $("#phoneCode").removeAttr("disabled");//启用按钮
      $("#phoneCode").val("重新发送");
    }
    else {
    	curCount--;
    	$("#phoneCode").val( + curCount + "秒");
    }
  }
	
	$("#phone").focus(function() {
		var $uname = $("#user input").val();
		$("#user").css("border-color", "black");
		$("#user").css("box-shadow", "4px 2px 3px rgb(5,69,69)");
		$("#user input").css("color", "black");
		$("#user img").attr("src", "img/user1.png");
	})
	$("#phone").blur(function() {
		var $uname = $("#user input").val();
		$("#user").css("border-color", "rgba(245, 220, 148, 0.8)");
		$("#user").css("box-shadow", "2px 2px 4px rgb(236, 170, 179)");
		$("#user input").css("color", "rgb(245, 220, 148)");
		$("#user img").attr("src", "img/User.png");
	});
	$("#name").focus(function() {
		var $uname = $("#user1 input").val();
		$("#user1").css("border-color", "black");
		$("#user1").css("box-shadow", "4px 2px 3px rgb(5,69,69)");
		$("#user1 input").css("color", "black");
		$("#user1 img").attr("src", "img/user1.png");
	})
	$("#name").blur(function() {
		var $uname = $("#user1 input").val();
		$("#user1").css("border-color", "rgba(245, 220, 148, 0.8)");
		$("#user1").css("box-shadow", "2px 2px 4px rgb(236, 170, 179)");
		$("#user1 input").css("color", "rgb(245, 220, 148)");
		$("#user1 img").attr("src", "img/User.png");
	});
	$("#pwd").focus(function() {
		$("#password").css("border-color", "black");
		$("#password").css("box-shadow", "4px 2px 3px rgb(5,69,69)");
		$("#password input").css("color", "black");
		$("#password img").attr("src", "img/pw.png");
	})
	$("#pwd").blur(function() {
		$("#password").css("border-color", "rgba(245, 220, 148, 0.8)");
		$("#password").css("box-shadow", "2px 2px 4px rgb(236, 170, 179)");
		$("#password input").css("color", "rgb(245, 220, 148)");
		$("#password img").attr("src", "img/password.png");
	})
	$("#input3").focus(function() {
		$(".mail").css("border-color", "black");
		$(".mail").css("box-shadow", "4px 2px 3px rgb(5,69,69)");
		$(".mail input").css("color", "black");
	})
	$("#input3").blur(function() {
		$(".mail").css("border-color", "rgba(245, 220, 148, 0.8)");
		$(".mail").css("box-shadow", "2px 2px 4px rgb(236, 170, 179)");
		$(".mail input").css("color", "rgb(245, 220, 148)");
	})
	$("#code").focus(function() {
		$(".yan").css("border-color", "black");
		$(".yan").css("box-shadow", "4px 2px 3px rgb(5,69,69)");
		$(".yan input").css("color", "black");
	})
	$("#code").blur(function() {
		$(".yan").css("border-color", "rgba(245, 220, 148, 0.8)");
		$(".yan").css("box-shadow", "2px 2px 4px rgb(236, 170, 179)");
		$(".yan input").css("color", "rgb(245, 220, 148)");
	})
})

function shouji(){
		$("#shouji").css("background-color","#D2691E")
		$("#shouji").css("color","#FFE4B5")
		$("#zhanghao").css("background-color","#FFE4B5")
		$("#zhanghao").css("color","#D2691E")
		$("#phoneLogin").css("display","block");
		$("#userLogin").css("display","none");
	}
	function zhanghao(){
		$("#zhanghao").css("background-color","#D2691E")
		$("#zhanghao").css("color","#FFE4B5")
		$("#shouji").css("color","#D2691E")
		$("#shouji").css("background-color","#FFE4B5")
		$("#phoneLogin").css("display","none");
		$("#userLogin").css("display","block");
	}